<HTML><HEAD>
<!--
    ----------------
    Matrix Reduction
    ----------------
-->

<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers

/*
    THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
    Copyright (c)1998 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

    Use at your own risk. No warranty is given or implied of the suitability 
    of this applet for any specific application. Neither Erica Sadun nor 
    Charles River Media will be held responsible for any unwanted effects 
    due to the use of this applet or any derivative. 
*/

// Yes -- it all works for larger matrices.  Expand to your heart's 
// content. The method is left as a simple exercise for the reader.


//---------------MATRIX I/O------------------

// Load Matrix into the Form.  This uses JavaScript's eval function
// for interpreting strings as JavaScript code
function showMatrix()
{
    for (var j = 0; j < matrix.length; j++)
      for (var i = 0; i < matrix.length; i++)
          eval("document.forms[0].J"+i+""+j+".value = "+matrix[i][j])
}

// Read the Matrix from the Form
function readMatrix()
{
    for (var j = 0; j < matrix.length; j++)
      for (var i = 0; i < matrix.length; i++)
          eval("matrix["+i+"]["+j+"]"+
              "= document.forms[0].J"+i+""+j+".value")
}

//-------------MATRIX CREATION----------------

// Create row of a given size
function matrixRow(row, side)
{
    for (var i = 0; i < side; i++) 
        this[i] = ((i == row) ? 1 : 0)
    this.length = side
    return this
}

// Create matrix of size: side x side
function makeMatrix(side)
{
    for (var i = 0; i < side; i++) 
        this[i] = new matrixRow(i, side)
    this.length = side
    return this
}

// Initialize and Load Matrix
function initMatrix()
{
    matrix = new makeMatrix(3)
    showMatrix()
}


//---------------MATRIX MATH----------------

function reduceMatrix(x)
{
    // determine size of a side
    mSize = matrix.length

    // turn matrix[x][x] into unit
    amount = matrix[x][x]
    for (var j = 0; j < mSize; j++)
        matrix[x][j] /= amount
    
    // subtract sucessive rows
    for (var j = x+1; j < mSize; j++)
    {
        // determine multiplier
        amount = matrix[j][x]
        for (var i = 0; i < mSize; i++)
            matrix[j][i] -= (matrix[x][i] * amount)
    }
    
    // recurse for remaining rows
    if ((x+1) < mSize) reduceMatrix(x+1)
}

//--------------UTILITY FUNCTION---------------

function doit()
{
    readMatrix()
    reduceMatrix(0)
    showMatrix()
}

<!-- done hiding --></SCRIPT></HEAD>

<BODY bgcolor="ffffff" onLoad="setTimeout('initMatrix()', 100)">

<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
    ALIGN = CENTER>Matrix Reduction</H1></FONT>

<FONT COLOR="770000">
    Matrices (two-dimensional arrays) are used by mathematicians,
    astronomers, graphics programmers and roboticists.  Here's a
    simple matrix manipulation example.
    Fill in the matrix below and press the [Reduce] button to
    mathematically reduce it to an upper triangular matrix.
</FONT></BLOCKQUOTE>

<BR><BR>

<CENTER><FORM>

<INPUT NAME="J00" SIZE=6>
    <INPUT NAME="J01" SIZE=6>
    <INPUT NAME="J02" SIZE=6><p>
<INPUT NAME="J10" SIZE=6>
    <INPUT NAME="J11" SIZE=6>
    <INPUT NAME="J12" SIZE=6><p>
<INPUT NAME="J20" SIZE=6>
    <INPUT NAME="J21" SIZE=6>
    <INPUT NAME="J22" SIZE=6><p>

<INPUT TYPE="BUTTON" VALUE="Reinit" onClick="initMatrix()">
<INPUT TYPE="BUTTON" VALUE="Reduce" onClick="doit()">

</FORM></CENTER>

<br><br>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
This applet introduces the use of two-dimensional arrays. These
matrices are created by storing a series of one-dimensional arrays
in a master array. Once created, the matrix can be addressed by
successive brackets, such as matrix[2][1], matrix[0][2], etc.<p>
The form is also addressed in row-column format.
This applet codes the indices into the form name. 
(e.g. J21, J02, etc).<p>
</FONT>

<FONT COLOR="770000"><PRE>
//-------------MATRIX CREATION----------------

// Create row of a given size
function matrixRow(row, side)
{
    for (var i = 0; i < side; i++) 
        this[i] = ((i == row) ? 1 : 0)
    this.length = side
    return this
}

// Create matrix of size: side x side
function makeMatrix(side)
{
    for (var i = 0; i < side; i++) 
        this[i] = new matrixRow(i, side)
    this.length = side
    return this
}

//-------------2-D FORM----------------
&lt;FORM&gt;

&lt;INPUT NAME="J00" SIZE=6&gt;
    &lt;INPUT NAME="J01" SIZE=6&gt;
    &lt;INPUT NAME="J02" SIZE=6&gt;&lt;p&gt;
&lt;INPUT NAME="J10" SIZE=6&gt;
    &lt;INPUT NAME="J11" SIZE=6&gt;
    &lt;INPUT NAME="J12" SIZE=6&gt;&lt;p&gt;
&lt;INPUT NAME="J20" SIZE=6&gt;
    &lt;INPUT NAME="J21" SIZE=6&gt;
    &lt;INPUT NAME="J22" SIZE=6&gt;&lt;p&gt;

&lt;INPUT TYPE="BUTTON" VALUE="Reinit" onClick="initMatrix()"&gt;
&lt;INPUT TYPE="BUTTON" VALUE="Reduce" onClick="doit()"&gt;

&lt;/FORM&gt;
</PRE></FONT>

<h5>Copyright &copy;1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>